package java8Test.function.lambda.part2;

import spring.po.User;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.TreeSet;
import java.util.function.Supplier;

/**
 * @author : yang.xp
 * @date : 2022/9/22 16:17
 * @description : supplier 主要是为了提供值，不需参数
 */
public class SupplierTest {

    private static final ThreadLocal<User> threadLocal = ThreadLocal.withInitial(() -> new User());
    private static final ThreadLocal<DateFormat> threadLocal2 = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd"));
    private static final ThreadLocal<TreeSet<User>> treeSetThreadLocal = ThreadLocal.withInitial(() -> new TreeSet<>((e1, e2) -> Float.compare(e1.getAge(), e2.getAge())));

    public static void main(String[] args) {
        User user = User.buildUser();
        SupplierTest supplierTest = new SupplierTest();
        supplierTest.test(() -> false);
        supplierTest.testUser(() -> {
            user.setAge(1009);
            return user;
        });
        for (int i = 0; i < 10; i++) {
            System.out.println(supplierTest.testUserBuild(User::new).hashCode());
        }

        for (int i = 0; i < 10; i++) {
            System.out.println("yyyy-MM-dd".hashCode());
            new Thread(() -> System.out.println("user -- " + threadLocal.get().hashCode())).start();
            new Thread(() -> System.out.println("date -- " + Thread.currentThread().getName() + ":" + threadLocal2.get())).start();

//            new Thread(() -> {
//                treeSetThreadLocal.get().add(User.buildUser());
//                System.out.println("tree1 -- " + treeSetThreadLocal.get().first().hashCode());
//                System.out.println("tree2 -- " + treeSetThreadLocal.get().hashCode());
//            }).start();
        }
    }

    public void test(Supplier<Boolean> booleanSupplier) {
        System.out.println(booleanSupplier.get());
    }

    public void testUser(Supplier<User> userSupplier) {
        System.out.println(userSupplier.get().toString());
    }

    public User testUserBuild(Supplier<User> userSupplier) {
        return userSupplier.get();
    }
}
